package servlets;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import structures.User;
import managers.DatabaseManager;
import managers.JSonManager;

public class CreateNewUserServlet extends HttpServlet {

	private static final long serialVersionUID = 1767626731693941925L;
	
	//method to insert
		protected void doPut(HttpServletRequest request, HttpServletResponse response){
			String name, surname, email, password, cap, city, address, jsonResult, payPalEmail;
			User user;
			JSonManager jSonManager;
			jSonManager = new JSonManager();
			name = request.getParameter("name");
			surname = request.getParameter("surname");
			email = request.getParameter("email");
			password = request.getParameter("password");
			cap = request.getParameter("cap");
			city = request.getParameter("city");
			address = request.getParameter("address");
			payPalEmail = request.getParameter("payPalEmail");
			
			DatabaseManager databaseManager;
			databaseManager = new DatabaseManager();
			databaseManager.connect();
			user = databaseManager.insertUser(name, surname, email, password, cap, city, address, payPalEmail);
			databaseManager.closeConnection();
			jsonResult = jSonManager.generateJSON(user);
			response.setContentType("application/json");
			response.setCharacterEncoding("UTF-8");
			try {
				//write the result as JSon
				response.getWriter().write(jsonResult);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	
}
